﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tracker.Models.DAL;
using tracker.Models.DTO;
using tracker.Models;

namespace tracker.Controllers
{
    public class HandlerGetTreeJsonController : Controller
    {
        //
        // GET: /HandlerGetTreeJson/
        [CallAjaxFilter]
        public JsonStringResult Index(string node, string id, string method)
        {
            string result = "";
            List<UserDevive> lstUserDevive = new List<UserDevive>();
            if (node != null && id != null && method != null)
            {
                UserLoginDAL da = new UserLoginDAL();
                if (node == "1")
                {
                    EUserLogin user;
                    
                    user = da.getByUserName(HttpContext.Session[Common.USER].ToString());
                    UserDevive ent = new UserDevive();
                    ent.iconCls = user.getUserType();
                    ent.id = user.SecurityCode;
                    ent.leaf = false;
                    ent.text = user.Username;

                    lstUserDevive.Add(ent);

                }
                else
                {
                    // Get all user by ParentCode
                    List<EUserLogin> lstUser = da.getUserLoginByParentCode(id);
                    if (lstUser != null)
                    {
                        foreach (EUserLogin user in lstUser)
                        {
                            UserDevive ent = new UserDevive();
                            ent.iconCls = user.getUserType();
                            ent.id = user.SecurityCode;
                            ent.leaf = false;
                            ent.text = user.Username;

                            lstUserDevive.Add(ent);
                        }
                    }

                    // Get all device of this node
                    DeviceDAL deviceDAL = new DeviceDAL();
                    List<EDevice> lstDevice = deviceDAL.getBySecurityCode(id);
                    foreach (EDevice dv in lstDevice)
                    {
                        UserDevive ent = new UserDevive();
                        ent.iconCls = "Tracker";
                        ent.id = "T" + dv.DeviceKeySecurity;
                        ent.leaf = true;
                        ent.text = dv.DeviceName;
                        lstUserDevive.Add(ent);
                    }

                }
            }
            result = Utilities.getJsonString(lstUserDevive,string.Empty, 2);
            return new JsonStringResult(result);  
        }
        
         
           
        }

    }

    public class UserDevive
    {
        public string id { get; set; }
        public string text { get; set; }
        public bool leaf { get; set; }
        public string iconCls { get; set; }
    }

